package com.lxk.controller;

import com.lxk.pojo.Admin;
import com.lxk.service.LoginService;
import com.lxk.util.MD5Util;
import com.lxk.vo.result.CodeMsg;
import com.lxk.vo.result.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author JohnSoybean
 * @create 2020/3/19 18:27
 * @description 管理员账号权限相关
 */
@Controller
public class LoginController {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private LoginService ls;

    @GetMapping("/main")
    public String toMain(HttpServletRequest req, Model model) {
        model.addAttribute("test", "testMain");
        return "mainPage";
    }

    @RequestMapping(value="/login", method=RequestMethod.GET)
    public String adminIndex(HttpServletRequest req) {
        HttpSession session = req.getSession();
        session.invalidate();
        return "index";
    }

    @RequestMapping(value="/logout", method=RequestMethod.GET)
    public String logOut(HttpServletRequest req) {
        HttpSession session = req.getSession();
        session.removeAttribute("adminName");
        session.removeAttribute("adminId");
        session.removeAttribute("authType");
        session.invalidate();
        return "redirect:login";
    }

    @RequestMapping(value="/checkLogin", method= RequestMethod.POST)
    public String checkLogin(String adminName, String pwdMd5, HttpServletRequest req, Model model) {
        log.info(pwdMd5);
        HttpSession session = req.getSession();
        int check = ls.checkLogin(adminName, pwdMd5);
        if(check==1) {
            Admin admin = ls.getAdminInfo(adminName);
            session.setAttribute("adminName", adminName);
            session.setAttribute("adminId", admin.getAdminId());
            session.setAttribute("authType", admin.getAuthType());
            return "redirect:main";
        }else if(check==-1){
            model.addAttribute("errMsg", "账号不存在！");
            return "index";
        }else {
            log.info(adminName+"密码错误！"+pwdMd5);
            model.addAttribute("errMsg", "密码错误！");
            return "index";
        }
    }

    @SuppressWarnings("unchecked")
    @Transactional
    @RequestMapping(value="/changepwd", method=RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public Result<Integer> changePwd(HttpServletRequest req, String opwd, String npwd, String cpwd) {
        HttpSession session = req.getSession();
        int adminId = (Integer) session.getAttribute("adminId");
        String pwd = MD5Util.string2MD5(ls.getPwd(adminId));
        if(opwd.equals(pwd)) {
            if(npwd.equals(cpwd)&&null!=npwd) {
                int upd = ls.changePwd(adminId, npwd);
                log.info("admin:"+adminId+",修改密码为:"+npwd);
                return Result.success(upd);
            }else {
                return Result.error(CodeMsg.PWD_NOT_SAME);
            }
        }else {
            return Result.error(CodeMsg.PASSWORD_ERR);
        }
    }



}
